現在、以下のコードに取り組んでおり、カスタム クラス名を追加したいと考えています。私はPHPに精通していません。
カスタム CSS クラス名を追加するにはどうすればよいですか?
<?php next_posts_link('Older Posts'); ?>
<?php previous_posts_link('Newer Posts'); ?>
add_filter('next_posts_link_attributes', 'posts_link_attributes');
add_filter('previous_posts_link_attributes', 'posts_link_attributes');
function posts_link_attributes() {
return 'class="styled-button"';
}
これをテーマの functions.php に追加します。これらのリンクにクラス「styled-button」を追加します。それらを区別したい場合は、次を使用します。
add_filter('next_posts_link_attributes', 'next_posts_link_attributes');
add_filter('previous_posts_link_attributes', 'prev_posts_link_attributes');
function next_posts_link_attributes() {
return 'class="next-styled-button"';
}
function prev_posts_link_attributes() {
return 'class="prev-styled-button"';
}
私は Wordpress に詳しくありませんが、Codex によると、これらの関数呼び出しでクラス属性を指定する方法はありません。
できることはstr_replace
、これらの関数によって返される htmlです。
echo str_replace('<a ', '<a class="" ', next_posts_link('Older Posts'));