Automate Unique ID On Class & Help Make Each Button Separately Modifiable - php

I'm using wordpress, I've a content box plugin WP Boxer, I would like to take control of each individual button in each box created.
So I wanted to create a separate class for every button, right now they are all under the same class.
Here's how it is now:
$box['content']['text'] = $excerpt. '<div class="clearboth"></div>
'. sprintf('%s »', __('Read More', WPBOXER_ADMIN_TEXTDOMAIN)). ''. trim( $block_link );
And this is what i tried but it didn't work
$box['content']['text'] = $excerpt. '<div class="clearboth"></div>'
$i = 0;
if ( count($links) > 0 ) {
foreach( $links as $id ) {
. sprintf('%s »', __('Read More', WPBOXER_ADMIN_TEXTDOMAIN)). ''. trim( $block_link );
$i++;
}

Try including
id="<?php echo $i; ?>"
inside the tag instead of putting it in the class

Related

Wordpress Portfolio changes using PHP

I have added project-types to my website portfolio https://littleseabear.com/portfolio but it lists all possible tags, and not the tags directly linked to the portfolio.
For example, Cast should only be film, screenplay and work in progress, while When the Boys Come Home should be Radio and Produced.
Here is my code:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_terms( 'jetpack-portfolio-type' );
if( is_archive() || is_home() || is_front_page() ){
?>
<div class="post-wrapper">
<?php
infinity_news_post_thumbnail();
}
?>
<div class="post-thumbnail">
<?php
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>, ';
}
?>
<div class="article-details <?php if( is_single() ){ echo 'single-article-details'; } ?>">
Also... is there a way to get rid of the commas? Thanks
get_terms retrieves all available tags, not only the ones associated to the current post. Instead, modify the get_terms line at the top like so:
$taxonomy = 'jetpack-portfolio-type' ;
$tax_terms = get_the_terms( get_the_ID(), $taxonomy );
This will collect terms of the $taxonomy associated to the current post.
Edit:
I missed the question about the commas. To totally get rid of those, you can just remove them from the output:
foreach ( $tax_terms as $tax_term ) {
echo '<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a> ';
}
I removed the , right behind the closing </a>, so it will now only be separated by whitespaces. If you want to have a special separator only between items, you can achieve this like so:
$separator = ''; // initialize empty
foreach ( $tax_terms as $tax_term ) {
echo $separator.'<a class="filter" href="/project-type/'. $tax_term->slug.'">' . $tax_term->name .'</a>';
if (empty($separator)) {
// Choose a custom separator HERE:
$separator = ', ';
}
}

Implementing a Wordpress custom function

Site I’m working on is a radio show. I have a bunch of custom fields, courtesy of Advanced Custom Fields, for links to musicians’ websites, social profiles, etc.. For the last few years, we’ve used a Genesis child theme to display them, and the code I adapted worked great. The radio show’s host/producer now wants to move to a non-Genesis theme, and I’m having some trouble making it work.
Here’s an example from the site we’re migrating FROM, which includes the functioning code. (The Artist Links box, floated to the right of the main copy.)
Here’s the same post at the new site, where the code doesn’t yet display. (It will display in the same place.) FWIW, the theme is GrandBlog.
Here’s the code:
<?php
add_action ('if_artist_links');
function if_artist_links() {
// These are the custom field keys defined in the dashboard:
$metas_links = array(
'website',
'album',
'tour',
'twitter',
'facebook',
'bandcamp',
'soundcloud',
'youtube',
'instagram',
'linkedin',
'myspace',
'image',
);
// If _any_ ACF field is filled in, it's a go.
$has_meta = false; // init
foreach ($metas_links as $test ) {
if ( get_field($test) ) {
$has_meta = true;
continue; // Just need one meta field filled to create the div.
}
}
if ( $has_meta ) {
echo '<div class="custom-data artist-links">';
echo '<h2 class="artistname">' ?> <?php the_field('name') ?></h2>
<center><strong> <?php the_field('tribe') ?></strong></center> <?php
foreach ( $metas_links as $meta_links ) {
$$meta_links = get_field($meta_links);
if ( $$meta_links ) {
$f_object = get_field_object($meta_links);
$label = $f_object['label'];
echo '<div class="' . $meta_links . '">' .
'<span class="label">' . $label . '</span>' .
'</div>';
}
}
echo '</div><!-- /.custom-data -->';
}
}
?>`
Where there’s an ACF value, a link is created. Where there’s no ACF value, there’s no link. Just one link is required to make the box appear.
As far as I can tell, I’m not telling the code to run, or run in the right way. If you can help me do that, I’ll sing your praises through the weekend.
If possible, I’d really like to wrap this up in a WP plugin, for simplicity and ease of maintenance, and to put minimal code in single.php.
Thanks so much for your help!
You need to use WordPress the_content hook. Replace this in your theme's 'functions.php'.
function single_add_to_content( $content ) {
if( is_single() ) {
// These are the custom field keys defined in the dashboard:
$metas_links = array(
'website',
'album',
'tour',
'twitter',
'facebook',
'bandcamp',
'soundcloud',
'youtube',
'instagram',
'linkedin',
'myspace',
'image',
);
// If _any_ ACF field is filled in, it's a go.
$has_meta = false; // init
foreach ($metas_links as $test ) {
if ( get_field($test) ) {
$has_meta = true;
continue; // Just need one meta field filled to create the div.
}
}
if ( $has_meta ) {
$content.= '<div class="custom-data artist-links">';
$content.= '<h2 class="artistname">'.get_field('name').'</h2>
<center><strong>'.get_field('tribe').'</strong></center>';
foreach ( $metas_links as $meta_links ) {
$$meta_links = get_field($meta_links);
if ( $$meta_links ) {
$f_object = get_field_object($meta_links);
$label = $f_object['label'];
$content.= '<div class="' . $meta_links . '">' .
'<span class="label">' . $label . '</span>' .
'</div>';
}
}
$content.= '</div><!-- /.custom-data -->';
}
}
return $content;
}
add_filter( 'the_content', 'single_add_to_content' );
Or you can copy 'single.php' to your child theme and add this code directly without function.

How can I turn this PHP snippet into a Wordpress shortcode?

This is a music website with multiple artist pages - one page per artist. New content is added as a post with a Wordpress tag to denote the artist. This is so that I can add a Wordpress loop on each artist page to show all the posts filtered with that artist's tag.
I've got the filtered loop working correctly, but unfortunately it's currently hardwritten inside the page template's HTML, so it's only filtering for one tag. I don't want to create a new page template for each artist, so I'd like to add it to my functions.php file instead, where I can instead create a new shortcode for each artist.
Here's the current code in my page template, which filters the loop for all posts with our seefour tag:
<?php
query_posts( "tag=seefour" );
if ( have_posts() ) { ?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php } ?>
<?php } ?>
<?php } ?>
I'm assuming the best option is to turn this into a seefour shortcode inside my functions.php file - how can I achieve this?
Bonus question: is this sustainable in the long run (with 30-50+ artists) or will it cause a lot of redundant code? Open to suggestions...
P.S. I know this kind of question has been answered already (starting with raw PHP), but since I'm starting with a mix of HTML/PHP (and I'm a PHP newb), I just can't get it to work. So my apologies for asking again.
First of all, you should never ever use query_posts(). It is internal WordPress function to create and maintain the main WordPress cycle. Using it, you can crash your site in unpredictable manner. You should use get_posts() or WP_Query instead.
To have your custom shortcode, add the following to your functions.php:
function showtag_shortcode( $atts ) {
$atts = shortcode_atts( array(
'tag' => '', // Default value.
), $atts );
$posts = get_posts( 'tag=' . $atts['tag'] );
if ( $posts ) {
$output = '';
foreach ( $posts as $post ) {
setup_postdata( $post );
$output .= '<div class="jd-box">';
$output .= '<a href="' . get_the_permalink( $post ) . '">';
$output .= get_the_post_thumbnail( $post );
$output .= '<div class="jd-overlay"></div>';
$output .= '<div class="jd-overlay-text">';
$output .= get_the_title( $post );
$output .= '</div>';
$output .= '</a>';
$output .= '</div>';
}
} else {
$output = 'no data';
}
wp_reset_postdata();
return $output;
}
add_shortcode( 'showtag', 'showtag_shortcode' );
This function created [showtag] shortcode with one parameter: tag. You can use this shortcode on any page as follows:
[showtag tag="seefour"]
[showtag tag="disco"]
etc. You will have posts with relevant tags to be shown in place of your shortcode.
Putting a whole loop in a shortcode will make it messy, I know you can use shortcodes in Widgets etc as well but I guess that's not what you're after.
If so, the best option would be to make this code a page template say Artist and pass a variable in URL i.e. http://example.com/artist?t=seefour and then use the following code in the page template.
<?php
/**
* Template Name: Artist
*/
query_posts( "tag=" . $_GET['t'] );
if ( have_posts() ) {
?>
<?php while ( have_posts() ) { ?>
<?php the_post(); { ?>
<div class="jd-box">
<a href="<?php the_permalink(); ?>">
<?php the_post_thumbnail( ); ?>
<div class="jd-overlay"></div>
<div class="jd-overlay-text">
<?php the_title(); ?>
</div>
</a>
</div>
<?php
}
}
}
?>
This can be used for any number of artists, totally flexible because it is dependant on a variable that is provided on run time (when accessed).

how to create tab menu in admin page wordpress

this is my update code
if (is_admin())
{
add_action('admin_menu', 'user_menu');
}
function user_menu()
{
add_menu_page('Pesananku', 'Pesananku', 'subscriber', 'userslug',
'user_funct',get_template_directory_uri().'/img/favicon.ico',22);
}
function user_funct()
{
?>
<h2 class="nav-tab-wrapper">
Tab #1
Tab #2
Tab #3
</h2>
<div id="tab1"><!--content tab1--></div>
<div id="tab2"><!--content tab2--></div>
<div id="tab3"><!--content tab3--></div>
<?php
}
i want 'user_funct()' create tab with styling wordpress like this
http://postimg.org/image/h3nttjhof/
how to create different content with link tab menu
Thanks
Here is how you can go about it;
First, you need to create a function that create the structure depending on the current tab selected:
function page_tabs( $current = 'first' ) {
$tabs = array(
'first' => __( 'First tab', 'plugin-textdomain' ),
'second' => __( 'Second tab', 'plugin-textdomain' )
);
$html = '<h2 class="nav-tab-wrapper">';
foreach( $tabs as $tab => $name ){
$class = ( $tab == $current ) ? 'nav-tab-active' : '';
$html .= '<a class="nav-tab ' . $class . '" href="?page=ipl_dbx_import_export&tab=' . $tab . '">' . $name . '</a>';
}
$html .= '</h2>';
echo $html;
}
Then in the callback function called to display the page which will contain the tabs, you need to use this function like that:
<?php
// Code displayed before the tabs (outside)
// Tabs
$tab = ( ! empty( $_GET['tab'] ) ) ? esc_attr( $_GET['tab'] ) : 'first';
page_tabs( $tab );
if ( $tab == 'first' ) {
// add the code you want to be displayed in the first tab
}
else {
// add the code you want to be displayed in the second tab
}
// Code after the tabs (outside)
?>
Also then you call the page, you can add a GET value called tab corresponding to the tab you want to display, e.g.:
admin.php?page=my_plugin_page_slug&tab=second

Wordpress: disable links for get_the_category_list function?

Hey my theme uses this function to display the categories a post has, but it also creates links which I would like to get rid of. I prefer php rather than a javascript solution.
Here is the code:
<p class="postmeta">
<?php if ( 'post' == get_post_type() ) : // Hide category and tag text for pages on Search ?>
<?php
/* translators: used between list items, there is a space after the comma */
$categories_list = get_the_category_list( __( ', ', 'gridster' ) );
if ( $categories_list && gridster_categorized_blog() ) :
?>
<?php /*printf( __( '%1$s', 'gridster' ), $categories_list );*/ echo $categories_list; ?>
<?php endif; // End if categories ?>
<?php endif; // End if 'post' == get_post_type() ?>
</p>
Link to the codex reference
How can I void those links?
or get rid of links all together from DOM (but this might create more work as the actual text is between the <a> tags
HTML
<p class="postmeta">
<a target="_blank" href="http://whatever.com/category/default/" title="View all posts in Default" rel="category tag">Default</a>
</p>
Thanks!!
If you just want a text-string returned and use the native get_the_categories() function, you could simply wrap it in PHP's strip_tags() function to remove all HTML that is returned:
echo strip_tags( get_the_category_list(__( ', ', 'gridster' ));
You can try modifying this to fit your needs:
<?php
foreach((get_the_category()) as $category) {
echo $category->cat_name . ' ';
}
?>
(From http://premium.wpmudev.org/blog/how-to-get-a-wordpress-category-name-without-the-link/ )
One option is to copy the original function and adapt to your needs. Modified to remove the <a> tags but not tested, compare to the original, adapt if needed, and add it to the theme's functions.php:
function category_list_so_22771587( $separator = '' ) {
global $wp_rewrite;
$categories = get_the_category( $post_id );
$thelist = '';
$i = 0;
foreach ( $categories as $category ) {
if ( 0 < $i )
$thelist .= $separator;
switch ( strtolower( $parents ) ) {
case 'multiple':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, true, $separator );
$thelist .= $category->name;
break;
case 'single':
if ( $category->parent )
$thelist .= get_category_parents( $category->parent, false, $separator );
$thelist .= $category->name;
break;
case '':
default:
$thelist .= $category->name;
}
++$i;
}
return $thelist;
}
And modify your template to:
$categories_list = category_list_so_22771587( __( ', ', 'gridster' ) );
You can retrieve categories, extract the 'name' column of each items, and implode each values with a comma:
<?= implode(', ', (array_column(get_the_category(), 'name')))?>
Or you can retrieve categories list with link, and remove a tags :
<?= strip_tags(get_the_category_list(', ')) ; ?>
Do you just want to hide the links? If so use css:
.postmeta a {display: none}
Edit, you can also "disable" those links in css:
.postmeta a {
pointer-events: none;
cursor: default;
}

Categories