回答編集履歴

1

回答内容の変更

2019/06/09 16:51

投稿

YukiYamashina
YukiYamashina

スコア1011

test CHANGED
@@ -1,3 +1,117 @@
1
+ > このwpp-cardのところにカテゴリを代表する(idなど)挿入できたらいいのですが、実際にはphpをphpで挟むことになってしまい、それができません
2
+
3
+
4
+
5
+ 当該プラグインでの HTML 出力時にカテゴリー情報を追加するためには、[wpp_parse_custom_content_tags](https://github.com/cabrerahector/wordpress-popular-posts/wiki/3.-Filters#wpp_parse_custom_content_tags) フィルターでカスタムコンテンツタグを追加する必要があるようです。例えば `{cat_ID}` というカスタムコンテンツタグとして `cat-id-XXX` (`XXX` はカテゴリーID) クラスを出力するためには
6
+
7
+
8
+
9
+ ```php
10
+
11
+ <?php
12
+
13
+ /**
14
+
15
+ * Parses custom content tags in WordPress Popular Posts.
16
+
17
+ *
18
+
19
+ * @param string $html The HTML markup from the plugin.
20
+
21
+ * @param integer $post_id The post/page ID.
22
+
23
+ * @return string
24
+
25
+ */
26
+
27
+ function wpp_parse_tags_in_popular_posts( $html, $post_id ) {
28
+
29
+ if ( false !== strpos( $html, '{cat_ID}' ) ) {
30
+
31
+ if ( $categories = get_the_category( $post_id ) ) {
32
+
33
+ $class = 'cat-id-' . $categories[0]->cat_ID;
34
+
35
+ $html = str_replace( '{cat_ID}', $class, $html );
36
+
37
+ }
38
+
39
+ }
40
+
41
+ return $html;
42
+
43
+ }
44
+
45
+ add_filter( 'wpp_parse_custom_content_tags', 'wpp_parse_tags_in_popular_posts', 10, 2 );
46
+
47
+ ```
48
+
49
+
50
+
51
+ でカスタムコンテンツタグの処理を追加し、
52
+
53
+
54
+
55
+ ```php
56
+
57
+ <?php
58
+
59
+ wpp_get_mostpopular(
60
+
61
+ array(
62
+
63
+ ...
64
+
65
+ 'post_html' => '
66
+
67
+ <div class="post-list-magazine">
68
+
69
+ <article class="post-list">
70
+
71
+ <div class = "wpp-card cat-name {cat_ID}">
72
+
73
+ <header>
74
+
75
+ <div class="post-thumbnail">{thumb}</div>
76
+
77
+ </header>
78
+
79
+ <section class="p">
80
+
81
+ <div class="wpp-card-title"> <h2>{title}</h2></div>
82
+
83
+
84
+
85
+ <span class="wpp-meta post-stats">{stats}</span>
86
+
87
+ </section>
88
+
89
+ </div>
90
+
91
+ </article>
92
+
93
+ </div>'
94
+
95
+ )
96
+
97
+ );
98
+
99
+ ```
100
+
101
+
102
+
103
+ のように `{cat_ID}` を使用してみてください。
104
+
105
+
106
+
107
+
108
+
109
+ 編集前
110
+
111
+ ---
112
+
113
+
114
+
1
115
  [文字列の結合演算子 "."](https://www.php.net/manual/ja/language.operators.string.php) を使うと実現できると思います。
2
116
 
3
117