Get all image/attachment IDs in Wordpress - php

I want to dislay wordpress gallery in single.php. I put this code into single.php
<?php echo do_shortcode('[gallery columns="4" ids="1,2,3,4,5,6,7,8"]');?>
The problem is that image ids are changing based on post ID.
I wonder what code should I put? I did:
<?php
$id_image = what_code_should_i_put_here;
echo do_shortcode('[gallery columns="4" ids="'.$id_image.'"]');
?>
But I have no idea the code part.
what_code_should_i_put_here must result '1,2,3,4,5,6,7,8'

Did you check this one: https://wordpress.stackexchange.com/questions/149268/get-all-image-ids-from-the-media-library? After you get IDs array, you would simply need to convert it to delimited string. Let me know if you need help on that.

Related

Wordpress database: add value of custom field to beginning of post (and save it combined)

I need to get the value of a custom field (_subtitle) from the post meta and have it saved to the beginning of the post text (post_content).
How can I achieve this? (I tried the update() function but lost the post content before)
You need to use get_post_meta() it might look something like that if you want to place it within a template.
<?php
$subs = get_post_meta(get_the_ID(),'_subtitle',true);
$post_content = get_the_content();
echo $subs.$post_content;
?>
This would be the gist of it, you could also filter the_content(); and place the code within the functions.php

Drupal hiding the title in a node if it's <none>

I'm trying to figure out how to hide the title of my nodes in drupal... I don't wanna use another module, I just want to change something in the node.tpl.php...
My attempt was to ask if the title is "" and if not, it should just post the title... I've did it like this:
Damn won't work to show the code here, got it in jsfiddle now: jsfiddle.net/8d6FR/
But it doesn't work. Has someone some suggestions why it won't work?
You code looks almost right, I've changed it slightly just to make it easier to understand:
<?php if($title!="<none>"){ print render($title_prefix); ?>
<h2<?php print $title_attributes; ?>><?php print $title; ?></h2>
<?php print render($title_suffix);} ?>
If that is not working then add the code:
var_dump($title!="<none>")
That will let you know how PHP is evaluating your if statement and will allow you to do some further debugging.
PHP in a JSFiddle, that is not gonna work :)...To hide your node titles you'll have to modify your tpl file indeed, or create one if it you need it for a certain content type or node, or manage the display of your nodes within Drupal admin. You may need Title to replace title field with a regular field in order to hide it with the "Manage display" interface or Display Suite.
Find your node.tpl.php or page.tpl.php (depends on your theme) using Theme Developper, and find something like print $title.

Get the category name,permalink and description

So I need to list some categories, and I can list them with wp_list_categories but i also need the description for that category, i need to do something like this:
Category name
The description here
And right now I'm stuck i don't know how to do this with wordpress
This page shows how to add the Category Description: http://codex.wordpress.org/Function_Reference/category_description
It should have everything you need there, but for quick reference, the simplest way is:
<?php echo category_description(); ?>
(within the Wordpress loop)
EDIT
Just realized since you're combining this with wp_list_categories(), you'll have to create a loop that goes through the categoriy lists and displays the title and the description.
Try this Wordpress support page: http://wordpress.org/support/topic/have-wp_list_categories-output-the-category-descpription
Solved:
<?php
foreach(get_categories() as $category):
echo $category->cat_name;
echo category_description($category->cat_ID);
endforeach;
?>

Wordpress loop export category list to php echo

Am a bit stuck with a wordpress loop, am wondering if anyone can help.
I need to run a Wordpress loop but only get the category names/id (Either is fine) from each post and have all of those variables as one php item I can echo later in the page.
Its for a category list filter system, but I only want to show categories which have posted displayed on that page.
The loop will be dynamic as well, so I cant just hard code exclude/include, I need to echo the value of all the numbers in together.
I hope that makes sense! Anyone who has any ideas would be really cool. Thanks!
I would use the get_the_category function like so...
<?php
// before you begin the wordpress loop
$category_array = array();
?>
<?php
// from *within* the wordpress loop
foreach((get_the_category()) as $category) {
if (!in_array($category->cat_name, $category_array)) {
$category_array[] = $category->cat_name;
}
}
?>
<?php
// after the wordpress loop is finished
echo implode(",", $category_array);
?>
This code basically creates a new (empty) array so that for each category in the current page, check if you've already added that category name to the array, then if not, go ahead and add it. Then when the loop is finished, echo out a comma separated string of category names. (You can of course, change the separator if you want a comma and space ", " or any other delimiter).
The Codex article has a lot more information on other things you can do with that function. Hope that helps.
Edit: Fixed the implementation because I forgot this was going to be used on a page where you are listing many posts using the loop. (You need to initialize your array from outside the wordpress loop, then echo your results after the loop has been finished).

Any idea why Wordpress's inCategorgy tag is not working?

So I am trying to add a "-none" to a class for a post if it is in a specific category in Wordpress. So like lets say if I am viewing a post that has a category id of 7, i want a certain class titled "example" change to "example-none".
Here's my code:
<div class="example<?= is_category('events') ?'-none':'' ?><?= in_category('7') ?'-none':'' ?>">
The weird thing with the code is, it works in a page when I am viewing all the posts in a specific category. But when I go to an interior post that is in a specific category, the code does not work.
I am using the in_category('7') tag to achieve this on a wordpress sidebar.
Any idea on what I am doing wrong?
I would remove the quotes around the id of the category:
in_category(7)
This should be a number, not a string.
Thanks. i got it working using this code:
<div class="example<? wp_reset_query(); ?><?= in_category(7) ?'-none':'' ?>">

Categories